package com.icoolkj.learn.juc.future;

import com.icoolkj.learn.juc.base.utils.SleepTools;
import lombok.extern.slf4j.Slf4j;

import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author icoolkj
 * @version 1.0
 * @description
 * @createDate 2025/03/05 15:11
 */
@Slf4j
public class CFDemo5 {
    public static void main(String[] args) {
//        CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
//            int number = new Random().nextInt(10);
//            log.info("第一阶段：{}", number);
//            return number;
//        }).thenAccept(number -> {
//            log.info("第二阶段: {}", number * 5);
//        });
//        try {
//            log.info("最终结果：{}", future.get());
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        } catch (ExecutionException e) {
//            throw new RuntimeException(e);
//        }



//        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
//            int number = new Random().nextInt(10) +1;
//            SleepTools.second(number);
//            log.info("第一阶段：{}", number);
//            return number;
//        });
//
//        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
//            int number = new Random().nextInt(10) +1;
//            SleepTools.second(number);
//            log.info("第二阶段：{}", number);
//            return number;
//        });
//
//        try {
//            future1.thenAcceptBoth(future2, (x, y) -> {
//                log.info("最终结果：{}", x + y);
//            }).get();
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        } catch (ExecutionException e) {
//            throw new RuntimeException(e);
//        }


//        CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
//            int number = new Random().nextInt(10);
//            log.info("第一阶段：{}", number);
//            return number;
//        }).thenRun(() -> {
//            log.info("thenRun 执行");
//        });
//
//        try {
//            log.info("最终结果：{}", future.get());
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        } catch (ExecutionException e) {
//            throw new RuntimeException(e);
//        }



        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
            int number = new Random().nextInt(10) +1;
            log.info("第一阶段：{}", number);
            return number;
        });

        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
            int number = new Random().nextInt(10) +1;
            log.info("第二阶段：{}", number);
            return number;
        });

        CompletableFuture<Integer> future3 = future1.thenCombine(future2, (x, y) -> {
            return x + y;
        });

        try {
            log.info("最终结果：{}", future3.get());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }



    }
}
